package main

import (
	"fmt"
	"gitee.com/chen-hongxun/msgo"
)

func Log(next msgo.HandlerFunc) msgo.HandlerFunc {
	return func(ctx *msgo.Context) {
		fmt.Println("打印请求参数")
		next(ctx)
		fmt.Println("返回执行时间")
	}
}
func main() {
	//http.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) {
	//	fmt.Fprintln(writer, "hello mszlu.com")
	//})
	//err := http.ListenAndServe(":8111", nil)
	//if err != nil {
	//	log.Fatal(err)
	//}
	engine := msgo.New()
	g := engine.Group("user")
	g.Get("/hello", func(ctx *msgo.Context) {
		fmt.Fprintln(ctx.W, "get hello mszlu.com")
	})

	g.Post("/hello", func(ctx *msgo.Context) {
		fmt.Fprintln(ctx.W, "post hello mszlu.com")
	})
	g.Post("/hello/name", func(ctx *msgo.Context) {
		fmt.Fprintln(ctx.W, "/hello/name mszlu.com")
	})

	g2 := engine.Group("merchant")

	g2.PreHandle(func(next msgo.HandlerFunc) msgo.HandlerFunc {
		return func(ctx *msgo.Context) {
			fmt.Println("pre handle 1")
			next(ctx)
			fmt.Println("pre handle 2")
		}
	})
	g2.Get("/hello", func(ctx *msgo.Context) {
		fmt.Println("执行中")
		fmt.Fprintln(ctx.W, "merchant get hello mszlu.com")
	}, Log)

	g2.Post("/hello", func(ctx *msgo.Context) {
		fmt.Fprintln(ctx.W, "merchant post hello mszlu.com")
	})
	g2.Post("/hello/name", func(ctx *msgo.Context) {
		fmt.Fprintln(ctx.W, "merchant /hello/name mszlu.com")
	})
	g2.Post("/hello2/:id/aa", func(ctx *msgo.Context) {
		fmt.Fprintln(ctx.W, "merchant post /hello2/:id mszlu.com")
	})
	g2.Post("/jjj/*/aa", func(ctx *msgo.Context) {
		fmt.Fprintln(ctx.W, "merchant post /jjj/*/aa mszlu.com")
	})
	engine.Run()
}
